package com.pda.xinma.ui.machinecall;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.pda.xinma.R;
import com.pda.xinma.bean.LatheMachinBean;
import com.pda.xinma.bean.LatheOrderBean;
import com.pda.xinma.bean.SelectBean;
import com.pda.xinma.bean.request.CallCarryRequest;
import com.pda.xinma.contract.CallMaterialContract;
import com.pda.xinma.dialog.AffirmDialog;
import com.pda.xinma.presenter.CallMaterialPresenter;
import com.pda.xinma.utils.PopUtisl;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CallMaterialFragment extends BaseMvpFragment<CallMaterialPresenter> implements CallMaterialContract.View {
    LatheOrderBean orderBean;
    @BindView(R.id.tv_container)
    TextView tvContainer;
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.tv_machine)
    TextView tvMachine;
    @BindView(R.id.tv_processName)
    TextView tvProcessName;
    @BindView(R.id.tv_articleId)
    TextView tvArticleId;
    @BindView(R.id.tv_model)
    TextView tvModel;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.tv_planNum)
    TextView tvPlanNum;
    @BindView(R.id.sb_scan)
    Button sbScan;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;
    @BindView(R.id.tv_detail)
    TextView tvDetail;

    public CallMaterialFragment(LatheOrderBean orderBean) {
        this.orderBean = orderBean;
    }

    public CallMaterialFragment() {
    }

    @Override
    public CallMaterialPresenter getPresenter() {
        return new CallMaterialPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_call_material;
    }

    @Override
    public void initView() {
        lathCode = orderBean.getLatheCode();
        tvCenter.setText(orderBean.getCenterName());
        tvContainer.setText(orderBean.getProOrderNo());
        tvArticleId.setText(orderBean.getArticleId());
        tvPlanNum.setText(orderBean.getPlanNum());
        tvProcessName.setText(orderBean.getProcessName());
        tvMachine.setText(orderBean.getLatheName());
        tvGrade.setText(orderBean.getGrade());
        tvModel.setText(orderBean.getModel());
        if (/*orderBean.getStatus().equals("已完成") || */orderBean.getStatus().equals("已叫料")) {
            sbScan.setVisibility(View.GONE);
            tvMachine.setFocusable(false);
            tvMachine.setClickable(false);
        }
    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {

    }

    @OnClick({R.id.sb_scan, R.id.tv_move_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sb_scan:
                CallCarryRequest request = new CallCarryRequest();
                request.setFlowId(orderBean.getFlowId());
                request.setLatheCode(lathCode);
                presenter.popUpWindow(request);

                break;
            case R.id.tv_move_out:
                startIntent(AppActivityKey.PICKINGACTIVITY);
                break;
        }
    }

    String lathCode;

    @OnClick(R.id.tv_machine)
    public void onViewClicked() {
        presenter.getLatheListByCenter(orderBean.getCenterCode());
    }

    @Override
    public void setLatheList(List<LatheMachinBean> list) {
        List<SelectBean> selectBeans = new ArrayList<>();
        for (LatheMachinBean bean : list) {
            selectBeans.add(new SelectBean(bean.getLatheName(), bean.getLatheCode()));
        }
        PopUtisl.showSelectPop(mContext, selectBeans, tvMachine, new PopUtisl.SelectClick() {
            @Override
            public void onClick(SelectBean bean) {
                lathCode = bean.getId();
                tvMachine.setText(bean.getName());
            }
        }, "请选择机台");
    }

    @Override
    public void setPopMsg(String msg) {
        AffirmDialog affirmDialog = new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
            @Override
            public void onConfrim() {
                CallCarryRequest request = new CallCarryRequest();
                request.setFlowId(orderBean.getFlowId());
                request.setLatheCode(lathCode);
                presenter.callCarry(request);
            }
        });
        affirmDialog.show(msg);
    }

    @OnClick(R.id.tv_detail)
    public void onViewClicked1() {
        if (TextUtils.isEmpty(tvContainer.getText().toString())) {
            showMessage("请先扫生产订单码");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString("order", tvContainer.getText().toString());
        startIntent(AppActivityKey.ORDERDETAILACTIVITY, bundle);
    }
}
